/* 点出统计 */

(function (win,doc) {
	//var uid = isLogin = '';//这里暴露了一个全局变量isLogin -by MaKai
	var uid ="", isLogin = '';
	if (win.J) {
		isLogin = +J.isLogin;
	}
	if (win.J16_INF) {
		uid = J16_INF.uid;
	}else if (win.parent.J16_INF) {
		uid = win.parent.J16_INF.uid;
	}else{
		uid = function (/*string*/n) {
			var c = document.cookie.split("; ");
			for (var i = 0; i < c.length; i++) {
				var p = c[i].split("=");
				if (n == p[0]) try { return decodeURIComponent(p[1]) } catch (e) { break; }
			}
			return '';
		}("Uid");
	}

	var bind = win.attachEvent ?
	function (dom, eventType, handler) { dom.attachEvent('on' + eventType, handler) } :
	function (dom, eventType, handler) { dom.addEventListener(eventType, handler, false) };
	var euc = encodeURIComponent;
	/*--点出统计函数--*/
	var stat = (
	new function () {
		//每次点出，创建一个img, 因为新版有可能出现两个统计同时发出的情况
		this.createImg = function (src) {
			(new Image).src = src;
		};

		this.s16 = function (a, p) {
			var wp = ["act=" + a, "log=" + isLogin, "uid=" + uid];
			for (var i = 0; i < p.length; i += 2) {
				wp.push(p[i] + "=" + euc(p[i + 1]));
			}
			if(wp){
				this.createImg("http://st.1616.net/s.php?" + wp.join("&"));
			}
		};

		this.init = function (wrap) {
			var T = this;
			bind(wrap, 'mousedown', function (e) {
				if (!e) e = window.event;
				e = e.target || e.srcElement;
				//<div class="twrap" s16="act=21,pn=购物内页">
				var getPn = function (dom) {
					var attr = dom.getAttribute('s16');
					if (attr === null && dom.parentNode && dom.parentNode != (wrap.body || wrap)) {
						return getPn(dom.parentNode);
					} else {
						if(attr){
							var ret = {}, a, attr = attr.split(',');
							while (a = attr.pop()) {
								if(a){
									a = a.split('=');
									if (a.length == 2) ret[a[0]] = a[1];
								}
							}
						}
					}
					return ret
				};
				//if(e.tagName === 'BODY'){return false;}; 
				var atr = 'getAttribute' in e && e.getAttribute('s16'); // input disabled时没getAttribute
				/*第一种，是完全自定义的统计，如首页的邮箱，它的结构是 <a href='#' s16='#act=1&t=mail&v=163'*/
				//@ 如 : http://st.1616.net/s.php?act=1&t=mail&v=163 --> 首页的邮箱
				if (atr && atr.match(/^#/)) {
					return T.createImg("http://st.1616.net/s.php?" + atr.replace('#', ''));
				}
				if (e.tagName == 'FONT' || e.tagName == 'IMG') e = e.parentNode;
				if (e.tagName == 'A') {
					var p = getPn(e);
					if (p !== null && p !== undefined ) {
						T.s16(p.act, ['pn', p.pn, 'u', e.href, 't', (p.title || e.textContent || e.innerText || "").replace(/ /g, '').replace('New!', '')]);
					}
				}
			});
			return this;
		};
	}).init(doc);
})(window,window.document);